package com.bangdream.michelia.presenter;

import android.app.ProgressDialog;

import com.bangdream.michelia.contract.RecommendContract;
import com.bangdream.michelia.entity.RecommendBean;
import com.bangdream.michelia.model.IRecommendModelImpl;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.rejava.BaseObserverEntityNormalHttp;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/1 20:57
 * @email 909732184@qq.com
 * 推荐服务
 */
public class RecommendPresenter<V> extends BasePresenter<V> {
    private static final String TAG = "RecommendPresenter";
    private RecommendContract.IRecommendModel model;

    public RecommendPresenter() {
        model = new IRecommendModelImpl();
    }
    public void getRecommendListData(final int pageNo, int pageSize, ProgressDialog dialog){
        if(getMyView()==null)
            return;
        setPd(dialog);
        //公开状态（ 0：个人 1.指定人员可见 2：公开）
        getRecommendListData(pageNo,pageSize,null,dialog);
    }

    /**
     *
     * @param pageNo
     * @param pageSize
     * @param paramsMap  查询条件
     * @param dialog
     */
    public void getRecommendListData(final int pageNo, int pageSize,Map paramsMap, ProgressDialog dialog){
        if(getMyView()==null)
            return;
        setPd(dialog);
        if(paramsMap==null){
            paramsMap = new HashMap();
        }
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
        //公开状态（ 0：个人 1.指定人员可见 2：公开）
        //paramsMap.put("publicState","2");
        Map finalParamsMap = paramsMap;
        TokenManagerDef.work(() -> model.getRecommendList(finalParamsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<RecommendBean>>(this, dialog) {
                    @Override
                    public void onHandleSuccess(List<RecommendBean> data) {
                        if(getMyView()==null)
                            return;
                        getMyView().setRecommendList(true,pageNo,data,getDescribe(data));
                    }
                });
    }
    private RecommendContract.IRecommendView getMyView() {
        return (RecommendContract.IRecommendView) obtainView();
    }
}
